package controllers;

import play.*;
import play.mvc.*;

import java.util.*;

import models.*;

public class Friends extends Controller {

	public static Set<User> getFriendsAndFriendOfCurrentUser() {
		User currentUser = Login.getCurrentUser();
		if (currentUser != null) {
			return currentUser.getFriendsAndFriendOf();
		}
		return null;
	}

	public static Set<User> getFriendsNotFriendOfCurrentUser() {
		User currentUser = Login.getCurrentUser();
		if (currentUser != null) {
			return currentUser.getFriendsNotFriendOf();
		}
		return null;
	}

	public static Set<User> getFriendOfNotFriendsCurrentUser() {
		User currentUser = Login.getCurrentUser();
		if (currentUser != null) {
			return currentUser.getFriendOfNotFriend();
		}
		return null;
	}
	
	public static void getFriendsAndFriendOfCurrentUserJSON() {
		Set<User> u = getFriendsAndFriendOfCurrentUser();
		if (u != null) {
			renderJSON(u, new UserSerializer());
		} else {
			renderJSON(false);
		}
	}
	
	public static void getFriendsNotFriendOfCurrentUserJSON() {
		Set<User> u = getFriendsNotFriendOfCurrentUser();
		if (u != null) {
			renderJSON(u, new UserSerializer());
		} else {
			renderJSON(false);
		}
	}
	
	public static void getFriendOfNotFriendsCurrentUserJSON() {
		Set<User> u = getFriendOfNotFriendsCurrentUser();
		if (u != null) {
			renderJSON(u, new UserSerializer());
		} else {
			renderJSON(false);
		}
	}
	
	public static void getFriendsJSON() {
		int type = Integer.valueOf(params.get("type"));
		Long pid = Long.valueOf(params.get("pid"));
		
		User u = User.findById(pid);
		if(u == null) {
			renderJSON(false);
			return;
		}
		
		Set<User> friends = u.getFriends(type);
		if (friends != null) {
			renderJSON(friends, new UserSerializer());
		} else {
			renderJSON(false);
		}
	}
	
	public static void currentUserAddFriend(User u) {
		User currentUser = Login.getCurrentUser();
		renderJSON(currentUser.addFriend(u));
	}
	
	public static void currentUserDeleteFriend(User u) {
		User currentUser = Login.getCurrentUser();
		renderJSON(currentUser.deleteFriend(u));
	}
	
	public static void currentUserHasFriend(User u) {
		User currentUser = Login.getCurrentUser();
		if(currentUser == null) {
			renderJSON("{\"state\":\"no current user\"}");
		} else if(currentUser.id == u.id) {
			renderJSON("{\"state\":\"self\"}");
		} else {
			if(currentUser.hasFriend(u)) {
				renderJSON("{\"state\":\"friend\"}");
			} else {
				renderJSON("{\"state\":\"no friend\"}");
			}
		}
	}
	
	public static void hasCurrentUserAsFriend(Long pid) {
		User currentUser = Login.getCurrentUser();
		User people = User.findById(pid);
		if(currentUser.id == pid) {
			renderJSON(false);
		} else if(people == null) {
			renderJSON(false);
		} else {
			renderJSON(people.hasFriend(currentUser));
		}
	}
	
	public static void hasFriend(Long subjId, Long objId) {
		if(subjId == objId) {
			renderJSON("{error:\"one may not friend self\"}");
		} else {
			User subj = User.findById(subjId);
			User obj = User.findById(objId);
			if(subj == null || obj == null) {
				renderJSON("{error:\"cannot find user\"}");
			} else {
				renderJSON(subj.hasFriend(obj));
			}
		}
	}

}